﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class ColumnsDocument : Document
    {
        #region Internal
        //Constructor
        internal ColumnsDocument(SqlServer.Database database)
        {
            DocumentType = DocumentType.Columns;
            RootDatabase = database;
            RootServer = RootDatabase.Server;
            URL = "index." + database.Name + "." + "columns.html";
            _database = database;
        }
        //Properties
        internal override string DisplayName { get { return "Columns"; } }
        //Methods
        internal override string GetHtml()
        {
            if (_html == "")
            {
                AddCssLine(@"      	.nameColumn {width: 300px; word-wrap: break-word;}");

                AddBodyLine(@"<div id=""mainBody"">");

                string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + ScrubHtml(RootServer.Name) +
                    @"</a> . ";
                heirarchy += @"<a href=""http://index." +
                    _database.Name + @".html"">" + ScrubHtml(_database.Name) + @"</a></h2>";
                AddBodyLine(heirarchy);

                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_database.Name) + @" Columns</h2>");

                #region Columns
                var tableColumns = (from schema in _database.Schemas
                                    from table in schema.Tables
                                    from column in table.Columns
                                    select column);
                var viewColumns = (from schema in _database.Schemas
                                   from view in schema.Views
                                   from column in view.Columns
                                   select column);
                var columns = (from column in tableColumns.Union(viewColumns)
                               orderby column.Name
                               select column);

                var columnsArray = columns.ToArray<SqlServer.Column>();

                AddBodyLine(@"<div id=""columns"">");
                AddBodyLine(@"<table>");
                AddBodyLine(@"<colgroup><col class=""nameColumn"" /><col class=""usedByColumn"" /></colgroup>");
                AddBodyLine(@"<tr><th>Name</th><th>Used by</th></tr>");

                Boolean evenRow = true;
                StringBuilder sb = new StringBuilder();
                for (int rowIndex = 0; rowIndex < columnsArray.Length; rowIndex++)
                {
                    //If this is the column in the list or the next column is different we need to start a new row.
                    if (rowIndex == 0 || columnsArray[rowIndex].Name != columnsArray[rowIndex - 1].Name)
                    {
                        if (evenRow == true)
                            sb.Append(@"<tr class=""evenRow"">");
                        else
                            sb.Append(@"<tr class=""oddRow"">");
                        evenRow = !evenRow;
                        sb.Append(@"<td>" + ScrubHtml(columnsArray[rowIndex].Name) + @"</td><td>");
                    }
                    //either way we need to add this column to the list.
                    if (columnsArray[rowIndex].Table != null)
                    {
                       if (Business.FilterPreferences.ShowTablesPage)
                       {
                          string href = @"<a href=""http://index." +
                              RootDatabase.Name + @"." + columnsArray[rowIndex].Table.Schema.Name + @".table." +
                              columnsArray[rowIndex].Table.Name + @".html"">" +
                              ScrubHtml(columnsArray[rowIndex].Table.Schema.Name) + "." +
                              ScrubHtml(columnsArray[rowIndex].Table.Name) + @"</a>,  ";
                          sb.Append(href);
                       }
                       else
                       {
                          string href = ScrubHtml(columnsArray[rowIndex].Table.Schema.Name) + "." +
                              ScrubHtml(columnsArray[rowIndex].Table.Name) + @",  ";
                          sb.Append(href);
                       }
                    }
                    else
                    {
                       if (Business.FilterPreferences.ShowViewsPage)
                       {
                          string href = @"<a href=""http://index." +
                              RootDatabase.Name + @"." + columnsArray[rowIndex].View.Schema.Name + @".view." +
                              columnsArray[rowIndex].View.Name + @".html"">" +
                              ScrubHtml(columnsArray[rowIndex].View.Schema.Name) + "." +
                              ScrubHtml(columnsArray[rowIndex].View.Name) + @"</a>,  ";
                          sb.Append(href);
                       }
                       else
                       {
                          string href = ScrubHtml(columnsArray[rowIndex].View.Schema.Name) + "." +
                              ScrubHtml(columnsArray[rowIndex].View.Name) + @",  ";
                          sb.Append(href);
                       }
                    }
                    
                    //Finally if this is the last column or the next column is different then we need to end this row
                    if (rowIndex == columnsArray.Length -1 || columnsArray[rowIndex].Name != columnsArray[rowIndex + 1].Name)
                    {
                        string column = sb.ToString();
                        column = column.TrimEnd(", ".ToCharArray());
                        AddBodyLine(column + @"</td></tr>");
                        sb = new StringBuilder();
                    }
                }
                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion

                AddBodyLine(@"</div>");
                _html = CreateHtmlDocument();
            }
            return _html;
        }
        #endregion
        #region Private
        private SqlServer.Database _database;
        #endregion
    }
}
